<?php
namespace app\massage\controller;
use app\AdminRest;
use app\massage\model\Article;

use app\massage\model\ArticleList;
use app\massage\model\Cap;
use app\massage\model\Commission;
use app\massage\model\CommShare;
use app\massage\model\Date;

use app\massage\model\OrderAddress;
use app\massage\model\OrderGoods;
use app\massage\model\RefundOrder;
use app\massage\model\SubData;
use app\massage\model\SubList;
use app\massage\model\User;
use app\massage\model\Wallet;
use longbingcore\wxcore\Excel;
use think\App;
use app\massage\model\Order as Model;


class AdminExcel extends AdminRest
{


    protected $model;

    protected $order_goods_model;

    protected $refund_order_model;

    protected $attendant_name;

    protected $comm_share_model;

    public function __construct(App $app) {

        parent::__construct($app);

        $this->model = new Model();

        $this->order_goods_model = new OrderGoods();

        $this->refund_order_model = new RefundOrder();

        $this->comm_share_model   = new CommShare();

        $this->attendant_name = getConfigSetting($this->_uniacid,'attendant_name');

    }


    /**
     * @author chenniang
     * @DataTime: 2021-03-15 14:43
     * @功能说明:列表
     */
    public function orderList(){

        $input = $this->_param;

        $dis[] = ['a.uniacid','=',$this->_uniacid];
        //时间搜素
        if(!empty($input['start_time'])&&!empty($input['end_time'])){

            $start_time = $input['start_time'];

            $end_time = $input['end_time'];

            $dis[] = ['a.create_time','between',"$start_time,$end_time"];
        }
        //商品名字搜索
        if(!empty($input['goods_name'])){

            $dis[] = ['c.goods_name','like','%'.$input['goods_name'].'%'];
        }
        //手机号搜索
        if(!empty($input['mobile'])){

            $order_address_model = new OrderAddress();

            $order_address_dis[] = ['mobile','like','%'.$input['mobile'].'%'];

            $order_id = $order_address_model->where($order_address_dis)->column('order_id');

            $dis[] = ['a.id','in',$order_id];
        }

        if($this->_user['is_admin']==0){

            $dis[] = ['a.admin_id','in',$this->admin_arr];
        }

        //合伙人
        if(!empty($input['partner_id'])){

            $dis[] = ['a.partner_id','=',$input['partner_id']];

        }

        if(!empty($input['admin_id'])){

            $dis[] = ['a.admin_id','=',$input['admin_id']];
        }

        if(!empty($input['pay_type'])){
            //订单状态搜索
            $dis[] = ['a.pay_type','=',$input['pay_type']];

        }else{
            //除开待转单
            $dis[] = ['a.pay_type','<>',8];

        }

        $map = [];
        //店铺名字搜索
        if(!empty($input['coach_name'])){

            $map[] = ['b.coach_name','like','%'.$input['coach_name'].'%'];

            $map[] = ['d.now_coach_name','like','%'.$input['coach_name'].'%'];
        }

        if(!empty($input['order_code'])){

            $dis[] = ['a.order_code','like','%'.$input['order_code'].'%'];
        }

        if(!empty($input['channel_cate_id'])){

            $dis[] = ['e.cate_id','=',$input['channel_cate_id']];

        }

        if(!empty($input['channel_name'])){

            $dis[] = ['e.user_name','like','%'.$input['channel_name'].'%'];
        }

        if(!empty($input['is_channel'])){

            $dis[] = ['a.pay_type','>',1];

            $dis[] = ['a.channel_id','<>',0];

        }
        //是否是加钟
        if(isset($input['is_add'])){

            $dis[] = ['a.is_add','=',$input['is_add']];

        }

        if(!empty($input['is_coach'])){

            if($input['is_coach']==2){

                $dis[] = ['a.coach_id','=',0];
            }else{

                $dis[] = ['a.coach_id','>',0];

            }

        }

        if(!empty($input['is_store'])){

            if($input['is_store']==1){

                $dis[] = ['a.store_id','>',0];
            }else{

                $dis[] = ['a.store_id','=',0];

            }

        }

        $data = $this->model->adminDataSelect($dis,$map,$this->_user['phone_encryption']);

        if(!empty($input['is_channel'])){

            if(!empty($input['is_add'])){

                $name = '渠道财务加单';

                $type = 2;

            }else{

                $name = '渠道财务订单';

                $type = 1;

            }

        }else{

            if(!empty($input['is_add'])&&$input['is_add']==1){

                $name = '加单列表';

                $type = 3;

            }else{

                $name = '订单列表';

                $type = 2;

            }

        }

        $attendant_name = $this->attendant_name;

        $header[] = '订单ID';
        $header[] = '服务项目';
        $header[] = '项目价格';
        $header[] = '项目数量';
        $header[] = '下单人';
        $header[] = '手机号';
        $header[] = $attendant_name;
        if(empty($input['is_add'])){
             $header[] = $attendant_name.'类型';
        }

        if(!empty($input['is_channel'])){

            $header[] = '渠道商';

            $header[] = '渠道';
        }

        $header[] = '服务方式';

        $header[] = '服务开始时间';

        if(empty($input['is_add'])){

            $header[] = '出行费用';
        }

        $header[] = '服务项目费用';

        if(empty($input['is_add'])){

            $header[] = '实收金额';
        }
        $header[] = '退款金额';

        if(empty($input['is_add'])){

            $header[] = '子订单号';

        }else{

            $header[] = '主订单号';

        }

        $header[] = '系统订单号';
        $header[] = '付款订单号';
        $header[] = $attendant_name.'所属上级';
        $header[] = '下单时间';
        $header[] = '支付方式';
        $header[] = '状态';

        $new_data = [];

        foreach ($data as $v){

            $info   = array();

            $info[] = $v['id'];

            $info[] = $v['goods_name'];

            $info[] = $v['price'];

            $info[] = $v['num'];

            $info[] = $v['user_name'];

            $info[] = $v['mobile'];

            $info[] = !empty($v['coach_info']['coach_name'])?$v['coach_info']['coach_name']:'';

            if(empty($input['is_add'])){

                $info[] = $v['coach_id']>0?'入驻'.$attendant_name:'非入驻'.$attendant_name;
            }

            if(!empty($input['is_channel'])){

                $info[] = $v['channel_name'];

                $info[] = $v['channel'];
            }

            $info[] = $v['store_id']>0?'到店服务':'上门服务';

            $info[] = date('Y-m-d H:i:s',$v['start_time']);

            if(empty($input['is_add'])) {

                $info[] = $v['car_price'];

            }

            $info[] = $v['init_service_price'];

            if(empty($input['is_add'])) {

                $info[] = $v['pay_price'];
            }

            $info[] = $v['refund_price'];

            if(empty($input['is_add'])){

                $info[] = !empty($v['add_order_id'][0]['order_code'])?$v['add_order_id'][0]['order_code']:'';

            }else{

                $info[] = !empty($v['add_pid']['order_code'])?$v['add_pid']['order_code']:'';;

            }

            $info[] = $v['order_code'];

            $info[] = $v['transaction_id'];

            $info[] = !empty($v['partner_id'])?$v['partner_name']:$v['admin_name'];

            $info[] = date('Y-m-d H:i:s',$v['create_time']);

            $info[] = $this->payModel($v['pay_model']);

            $info[] = $this->orderStatusText($v['pay_type']);

            $new_data[] = $info;
        }


        $excel = new Excel();

        $excel->excelExport($name,$header,$new_data,'',$type);

        return $this->success($data);

    }


    /**
     * @author chenniang
     * @DataTime: 2022-12-26 21:59
     * @功能说明:支付方式
     */
    public function payModel($type){

        switch ($type){

            case 1;

                $text = '微信支付';
                break;

            case 2;

                $text = '余额支付';
                break;
            case 3;

                $text = '支付宝支付';
                break;

            default:

                $text = $type;
                break;

        }

        return $text;

    }


    /**
     * @author chenniang
     * @DataTime: 2021-03-30 16:32
     * @功能说明:
     */
    public function orderStatusText($status){

        $attendant_name = $this->attendant_name;

        switch ($status){

            case 1:
                return '待支付';

                break;
            case 2:
                return '待服务';

                break;
            case 3:
                return $attendant_name.'接单';

                break;
            case 4:
                return $attendant_name.'出发';

                break;
            case 5:
                return $attendant_name.'到达';

                break;
            case 6:
                return '服务中';

                break;

            case 7:
                return '已完成';

                break;
            case 8:
                return '待转单';

                break;

            case -1:
                return '已取消';

                break;

        }

    }




    /**
     * @author chenniang
     * @DataTime: 2022-12-15 12:05
     * @功能说明:提交内容导出
     */
    public function subDataList(){

        $input = $this->_param;

        $article_model = new ArticleList();

        $sub_list_model= new SubList();

        $sub_data_model= new SubData();

        $article_title = $article_model->where(['id'=>$input['article_id']])->value('title');
        //获取导出标题
        $title_data = $article_model->getFieldTitle($input['article_id']);

        $title = ['用户ID','微信昵称'];

        $title = array_merge($title,array_column($title_data,'title'));

        $title[] = '提交时间';

        $name  = '文章表单数据导出-'.$article_title;

        $diss[] = ['article_id','=',$input['article_id']];

        $diss[] = ['status','=',1];

        if(!empty($input['start_time'])&&!empty($input['end_time'])){

            $diss[] = ['create_time','between',"{$input['start_time']},{$input['end_time']}"];

        }

        if(!empty($input['id'])){

            $diss[] = ['id','in',$input['id']];

        }

        $list = $sub_list_model->where($diss)->order('id desc')->select()->toArray();

        $new_data = [];

        if(!empty($list)){

            $user_model = new User();

            foreach ($list as &$v){

                $user_info = $user_model->where(['id'=>$v['user_id']])->field('nickName,avatarUrl')->find();

                $info   = array();

                $info[] = $v['user_id'];

                $info[] = $user_info['nickName'];

                if(!empty($title_data)){

                    foreach ($title_data as $vs){

                        $dis = [

                            'field_id' => $vs['field_id'],

                            'sub_id'   => $v['id']
                        ];

                        $find = $sub_data_model->where($dis)->value('value');;

                        $info[] = !empty($find)?$find:'';
                    }
                }

                $info[] = date('Y-m-d H:i:s',$v['create_time']);

                $new_data[] = $info;

            }

        }

        $excel = new Excel();

        $excel->excelExport($name,$title,$new_data);

        return $this->success(true);

    }




    /**
     * @author chenniang
     * @DataTime: 2023-04-18 11:31
     * @功能说明:财务报表
     */
    public function financeDetailedList(){

        $input = $this->_param;

        $dis[] = ['a.uniacid','=',$this->_uniacid];

        $dis[] = ['a.pay_type','=',7];

        if(!empty($input['start_time'])&&!empty($input['end_time'])){

            $dis[] = ['a.can_tx_date','between',"{$input['start_time']},{$input['end_time']}"];

        }
        if($this->_user['is_admin']==0){

            $dis[] = ['a.admin_id','in',$this->admin_arr];
        }

        if(!empty($input['order_code'])){

            $dis[] = ['a.order_code','like','%'.$input['order_code'].'%'];

        }

        $type = !empty($input['type'])?$input['type']:0;

        if(!empty($input['top_name'])){

            $id = $this->model->getFinanceOrderId($input['top_name'],$type);

            $dis[] = ['a.id','in',$id];
        }

        if(!empty($input['type'])){

            if($input['type']==2){

                $dis[] = ['b.type','in',[2,5,6]];

            }else{

                $dis[] = ['b.type','=',$input['type']];

            }
        }

        $data = $this->model->financeDetailedSelect($dis);

        $name = '财务报表';

        $header=[
            '系统订单号',
            '付款订单号',
            '入账时间',
            '入账账号',
            '财务类型',
            '支付金额',
            $this->attendant_name.'名称',
            $this->attendant_name.'提成',
            $this->attendant_name.'车费',
            '省代名称',
            '省代提成',
            '市代名称',
            '市代提成',
            '区代名称',
            '区代提成',
            '分销员姓名',
            '分销提成',
            '经纪人姓名',
            '经纪人提成',
            '业务员姓名',
            '业务员提成',
            '渠道商姓名',
            '渠道商提成',
            '平台利润',
        ];

        $new_data = [];

        if(!empty($data)){

            foreach ($data as $v){

                $info   = array();

                $info[] = $v['order_code']."\t";

                $info[] = $v['transaction_id']."\t";

                $info[] = date('Y-m-d H:i:s',$v['end_time']);

                $info[] = $this->payModel($v['pay_model']);

                $info[] = $v['is_add']==1?'加钟':'销售';

                $info[] = $v['pay_price'];

                $info[] = !empty($v['coach_name'])?$v['coach_name']:'';

                $info[] = !empty($v['coach_cash'])?$v['coach_cash']:0;

                $info[] = $v['car_price'];

                $info[] = !empty($v['province_name'])?$v['province_name']:'';

                $info[] = $v['province_cash'];

                $info[] = $v['city_name'];

                $info[] = $v['city_cash'];

                $info[] = $v['district_name'];

                $info[] = $v['district_cash'];

                $info[] = $v['cash_user_name'];

                $info[] = $v['user_cash'];

                $info[] = $v['partner_name'];

                $info[] = $v['partner_cash'];

                $info[] = $v['salesman_name'];

                $info[] = $v['salesman_cash'];

                $info[] = $v['channel_name'];

                $info[] = $v['channel_cash'];

                $info[] = $v['remain_cash'];

                $new_data[] = $info;
            }

        }

        $excel = new Excel();

        $excel->excelCsv($name,$header,$new_data);

       // return $this->success($data);
    }



    /**
     * @author chenniang
     * @DataTime: 2021-08-28 23:03
     * @功能说明:佣金记录
     */
    public function commList(){

        $input = $this->_param;

        $dis[] = ['a.uniacid','=',$this->_uniacid];

        $dis[] = ['a.cash','>',0];

        if(!empty($input['status'])){

            $dis[] = ['a.status','=',$input['status']];
        }else{

            $dis[] = ['a.status','>',-1];

        }

        $where = [];

        if(!empty($input['top_name'])){

            $where[] = ['c.nickName','like','%'.$input['top_name'].'%'];

            $where[] = ['e.agent_name','like','%'.$input['top_name'].'%'];

            $where[] = ['f.coach_name','like','%'.$input['top_name'].'%'];

            $where[] = ['g.now_coach_name','like','%'.$input['top_name'].'%'];

            $where[] = ['h.user_name','like','%'.$input['top_name'].'%'];

            $where[] = ['m.user_name','like','%'.$input['top_name'].'%'];
        }

        if($this->_user['is_admin']==0){

            $dis[] = ['a.admin_id','in',$this->admin_arr];

        }

        if(!empty($input['type'])){

            if($input['type']==2){

                $dis[] = ['a.type','in',[2,5,6]];
            }else{

                $dis[] = ['a.type','=',$input['type']];
            }

        }

        if(!empty($input['order_code'])){

            $dis[] = ['d.order_code','=',$input['order_code']];

        }

        $comm_model = new Commission();

        $user_model = new User();

        $data = $comm_model->recordSelect($dis,$where);

        $commission_custom = getConfigSetting($this->_uniacid,'commission_custom');

        if(!empty($data)){

            foreach ($data as &$v){

                $v['nickName'] = $user_model->where(['id'=>$v['user_id']])->value('nickName');

                $v['coach_cash_control'] = $v['status']==2&&$v['admin_id']==0&&in_array($v['type'],[3,8])&&$v['top_id']==0?1:0;

                $v['cash'] = round($v['cash'] ,2);

                if($v['car_price']>0){

                    $v['pay_price'] = $v['pay_price'].'(含车费'.$v['car_price'].')';
                }

                if(in_array($v['type'],[2,5,6,11])){

                    $v['top_name'] = $v['admin_name'];

                }elseif (in_array($v['type'],[3,8])){

                    $v['top_name'] = $v['coach_name'];

                    if($v['top_id']==0&&$v['car_cash']>0){

                        $v['cash'] = $v['cash'].'(含车费'.$v['car_cash'].')';

                    }

                    if(empty($v['top_id'])){

                        $v['top_name'] = $v['now_coach_name'];
                    }

                }elseif ($v['type']==10){

                    $v['top_name'] = $v['channel_name'];

                }elseif ($v['type']==12){

                    $v['top_name'] = $v['salesman_name'];
                }

                if($v['type']==2){

                    if($commission_custom==0){

                        $v['balance'] = '平台抽成-'.$v['balance'];
                    }

                    $coach_cash = $v['coach_cash']>0?'包含'.$v['coach_cash'].'线下服务费，':'';

                    $car_cash   = $v['car_cash']>0?'包含'.$v['car_cash'].'线下车费':'';

                    $v['cash'] = !empty($coach_cash)||!empty($car_cash)?$v['cash'].'('.$coach_cash.$car_cash.')':$v['cash'];
                }

                $share_cash = 0;
                //技师查询是否有分摊金额
                if($v['type']==3){

                    $share_cash = $this->comm_share_model->where(['order_id'=>$v['order_id'],'type'=>1])->sum('share_cash');
                }
                //代理商分摊金额
                if(in_array($v['type'],[2,5,6])){

                    $share_cash = $this->comm_share_model->where(['order_id'=>$v['order_id'],'type'=>2,'share_id'=>$v['top_id']])->sum('share_cash');
                }

                if(!empty($share_cash)){

                    $v['cash'] .= '  被分摊金额:'.$share_cash.'元';
                }

            }

        }

        $name = '分销佣金';

        $header=[
            'ID',
            '佣金获得者',
            '来源',
            '系统订单号',
            '付款订单号',
            '佣金类型',
            '状态',
            '提成比例',
            '订单总金额',
            '此单提成金额',
            '时间',
        ];
        foreach ($data as $v){

            $info   = array();

            $info[] = $v['id'];

            $info[] = $v['top_name'];

            $info[] = $v['nickName'];

            $info[] = $v['order_code']."\t";

            $info[] = $v['transaction_id']."\t";

            $info[] = $this->getCommType($v['type']);

            $info[] = $v['status']==2?'已到账':'未到账';

            if($v['type']==1){

                if(!empty($v['order_goods'])){

                    $str = '';

                    foreach ($v['order_goods'] as $vs){

                        $str .= $vs['goods_name'].'比例'.$v['balance'].'%,';

                    }
                }

                $info[] = $str;

            }else{

                $info[] = $v['balance'].'%';

            }

            $info[] = $v['pay_price'];

            $info[] = $v['cash'];

            $info[] = date('Y-m-d H:i:s',$v['create_time']);

            $new_data[] = $info;
        }

        $excel = new Excel();

        $excel->excelCsv($name,$header,$new_data);

       // return $this->success($data);

    }


    /**
     * @author chenniang
     * @DataTime: 2023-04-21 17:28
     * @功能说明:获取佣金类型
     */
    public function getCommType($type){

        $name = $this->attendant_name;

        $arr = [

            1 =>'分销商',

            2 =>'代理商',

            3 => $name,

            5 => '代理商',

            6 => '代理商',

            7 => $name.'拉用户充值余额',

            8 => '车费',

            9 => $name.'经纪人',

            10 => '渠道商',

            11 => '平台',

            12 => '业务员',

        ];

        return $arr[$type];

    }
















}
